-- IupSubmenu Example in IupLua 
-- Creates a dialog with a menu with three submenus. One of the submenus has a submenu, which has another submenu. 

require( "iuplua51" )
local http = require( "socket.http" )

currentPackRaw = ""
sensor = {}
-- Creates a text, sets its value and turns on text readonly mode 
text = iup.text {value = currentPackRaw, expand = "YES"}

-- Creates items of menus
item_exit = iup.item {title = "Exit"}
item_pack = iup.item {title = "Get ID packs..."}
item_help = iup.item {title = "Help"}

-- Creates menus of the main menu
menu_file = iup.menu {item_exit}
menu_discovery = iup.menu {item_pack}
menu_help = iup.menu {item_help}

-- Creates submenus of the main menu
submenu_file = iup.submenu {menu_file; title = "File"}
submenu_discovery = iup.submenu {menu_discovery; title = "Discovery"}
submenu_help = iup.submenu {menu_help; title = "Help"}

descLabel = iup.label{}
nameLabel = iup.list{dropdown = "YES", action = function (t,p,s) print(sensor.Desc); descLabel.title = sensor.Desc; dlg:showxy (iup.CENTER,iup.CENTER) end }
funcLabel = iup.list{dropdown = "YES"}
callbtn = iup.button{title = "Call function"}
hBox = iup.hbox{
	iup.vbox{
		iup.hbox{
			iup.label{title = "Sensor name:"},
			nameLabel,
			MARGIN = "0x0"
		},
		iup.label{title = "Description:"},
		descLabel,
		iup.fill{size = "3"},
		iup.hbox{
			iup.label{title = "Data service Fnc:"},
			funcLabel,
			MARGIN = "0x0"
		},
		callbtn,
	},
	text;
	MARGIN = "3x3";
	title = "HBOX"
}
-- Creates main menu with file submenu
menu = iup.menu {submenu_file, submenu_discovery, submenu_help}
                                
-- Creates dialog with a text, sets its title and associates a menu to it 
dlg = iup.dialog {hBox
      ; title ="VHeWS Demo client application", menu = menu, size = "400x200"}

-- Shows dialog in the center of the screen 
dlg:showxy (iup.CENTER,iup.CENTER)

function item_pack:action()

	ip = iup.text{}
	port = iup.text{}
	sID = iup.text{}
	iup.Popup(
		iup.dialog{
			iup.vbox{
				iup.hbox{
					iup.label{title="Host IP:"},
					iup.fill{},
					ip,
				},
				iup.hbox{
					iup.label{title="Port:"},
					iup.fill{},
					port,
				},
				iup.hbox{
					iup.label{title="Sensor ID:"},
					iup.fill{},
					sID,
				},
				iup.button{title = "Get ID packs",action = function () 
																			currentPackRaw,b = http.request("http://"..ip.value..":"..port.value.."/discovery.lua?sID="..sID.value)
																			print(currentPackRaw)
																			for k,v in string.gmatch(currentPackRaw, "(.-)=(.-)<br>\n") do
																				sensor[k] = v
																				print(k)
																			end
																			print(sensor.Name)
																			nameLabel.APPENDITEM = sensor.Name
																			funcLabel.APPENDITEM = sensor.FuncNames
																			dlg:showxy (iup.CENTER,iup.CENTER)
																			return iup.CLOSE 
																			end};
			};
			size = "100x50",
			RESIZE = "NO"
		}
	)
end

function callbtn:action()
	local t = ""
	t,b = http.request("http://"..ip.value..":"..port.value.."/dataservice.lua?fname="..funcLabel[1].."&IP="..sensor.IP)
	text.value = string.match(t,"%d%d/%d%d/%d%d %d%d:%d%d:%d%d average:%d")
end
function item_help:action ()
  iup.Message ("Warning", "Only Help and Exit items performs an operation")
  return iup.DEFAULT
end

function item_exit:action ()
  return iup.CLOSE
end

if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
  iup.MainLoop()
end
